﻿using ToolKitManagerIESiteList.Util;
using System;
using System.Reflection;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;

namespace ToolKitManagerIESiteList
{
    partial class AboutBox : Form
    {
        private BackgroundWorker BackWork;
        private List<string> Dependences;

        public AboutBox()
        {
            InitializeComponent();
        }

        private void AboutBox1_Load(object sender, EventArgs e)
        {
            var oldcur = this.Cursor;
            this.Cursor = Cursors.WaitCursor;
            Text = string.Format("About {0}", AssemblyHelper.AssemblyTitle);
            labelProductName.Text = AssemblyHelper.AssemblyProduct;
            labelVersion.Text = string.Format("Version {0}", AssemblyHelper.AssemblyVersion);
            labelCopyright.Text = AssemblyHelper.AssemblyCopyright;
            labelCompanyName.Text = AssemblyHelper.AssemblyCompany;
            textBoxDescription.Text = AssemblyHelper.AssemblyDescription;
            lnkLabel.Text = AssemblyHelper.AppSite;
            Dependences = new List<string>();
            BackWork = new BackgroundWorker();
            BackWork.DoWork += BackWork_DoWork;
            BackWork.RunWorkerCompleted += BackWork_RunWorkerCompleted;
            LstAss.Items.Clear();
            LstAss.Items.Add(Properties.Resources.WaitDependences);
            BackWork.RunWorkerAsync();
            this.Cursor = oldcur;
        }

        private void BackWork_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            LstAss.Items.Clear();
            foreach (var item in Dependences)
            {
                LstAss.Items.Add(item);
            }
        }

        private void BackWork_DoWork(object sender, DoWorkEventArgs e)
        {
            Dependences = new List<string>();
            foreach (var assemblyName in Assembly.GetExecutingAssembly().GetReferencedAssemblies())
            {
                if ((BackWork.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                Assembly assembly = Assembly.Load(assemblyName);
                if (!assembly.GlobalAssemblyCache)
                {
                    var value = string.Format("{0} : {1}", assemblyName.Name, assemblyName.Version);
                    if (!Dependences.Contains(value))
                    {
                        if (!Dependences.Contains(value))
                        {
                            Dependences.Add(value);
                        }
                        foreach (var item in assembly.GetReferencedAssemblies())
                        {
                            
                            if ((BackWork.CancellationPending == true))
                            {
                                e.Cancel = true;
                                break;
                            }
                            assembly = Assembly.Load(item);
                            if (!assembly.GlobalAssemblyCache)
                            {
                                value = string.Format("{0} : {1}", item.Name, item.Version);
                                if (!Dependences.Contains(value))
                                {
                                    Dependences.Add(value);
                                }
                            }
                        }
                    }
                }
            }
        }

        private void AboutBox_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (BackWork.IsBusy)
                BackWork.CancelAsync();
            while (BackWork.IsBusy)
                System.Threading.Thread.Sleep(500);
        }

        private void lnkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(lnkLabel.Text);
        }
    }
}
